library(here)

library(data.table)
library(rnaturalearth)
library(rnaturalearthdata)
library(ggplot2)

# Prep map data ---------------------------------------------------------------------

world <- ne_countries(scale = "medium", returnclass = "sf")


# Load competition data -------------------------------------------------------------

ml_data <- fread(here('data', 'full_rol.csv'))

## Contract-intensive trade ---------------------------------------------------------

fullData <- data.table(year = integer(),
                       exporter = integer(),
                       country = integer(),
                       corr = numeric())

for (yr in 1996:2020) {
  temp <- fread(here('data', 'trade', 'n', paste0(yr, '.csv')))
  temp$year <- yr
  
  fullData <- rbind(fullData, temp)
}
rm(temp, yr)

fullData <- fullData[exporter != country]

countrycodes <- fread(here('data', 'trade','country_codes_V202201.csv'))

fullData <- merge(fullData, countrycodes[ , .(exporter = country_code,
                                              exporter_iso3c = iso_3digit_alpha)],
                  by = 'exporter',
                  all.x = TRUE)

fullData <- merge(fullData, countrycodes[ , .(country = country_code,
                                              country_iso3c = iso_3digit_alpha)],
                  by = 'country',
                  all.x = TRUE)

fullData <- fullData[ , .(exporter, exporter_iso3c, 
                          country, country_iso3c, 
                          year, corr)]
setorder(fullData, exporter, country)

fullData <- merge(fullData, ml_data[ , .(country_iso3c = iso3c, year, 
                                         country_ml = ml)],
                  by = c('country_iso3c', 'year'),
                  all.x = TRUE)

fullData <- fullData[!is.na(fullData$country_ml)]

fullData[ corr < 0 , corr := 0]
fullData[ , corr_sum := sum(corr, na.rm = T) , by = .(exporter, year)]

fullData[ , weight := (corr / corr_sum), by = .(exporter, year)]

fullData <- fullData[, .(weight_mean = mean(weight, na.rm = T)) ,  by = .(country_iso3c, exporter_iso3c)]

# Figure 8a: Thailand ---------------------------------------------------------------

country <- "THA"
plotData <- merge(world, fullData[country_iso3c == country, .(iso_a3 = exporter_iso3c,
                                                              weight_mean)],
                  all.x = TRUE)

plotData$weight[is.na(plotData$weight)] <- 0
plotData$weight[plotData$iso_a3 == country] <- NA

ggplot(plotData) +
  geom_sf(aes(fill = weight), 
          size = .1) +
  scale_fill_gradient(name = 'Export competition weight',
                      low = 'white', high = 'black', 
                      na.value = 'darkred') +
  theme_bw() +
  theme(legend.position = 'bottom')

ggsave(here('output', 'figures', 'figure-7a.pdf'),
       width = 7, height = 4, units = 'in')  

# Figure 8a: South Korea ------------------------------------------------------------

country <- "KOR"
plotData <- merge(world, fullData[country_iso3c == 'KOR', .(iso_a3 = exporter_iso3c,
                                                              weight_mean)],
                  all.x = TRUE)

plotData$weight[is.na(plotData$weight)] <- 0
plotData$weight[plotData$iso_a3 == country] <- NA

ggplot(plotData) +
  geom_sf(aes(fill = weight), 
          size = .1) +
  scale_fill_gradient(name = 'Export competition weight',
                      low = 'white', high = 'black', 
                      na.value = 'darkred') +
  theme_bw() +
  theme(legend.position = 'bottom')

ggsave(here('output', 'figures','figure-7b.pdf'),
       width = 7, height = 4, units = 'in')  
